import { createBannerList, getBannerList, deleteBanner, changeBanner } from './advertisingListServ'
// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
// 引入接口文件
import { getAdvertising } from './advertisingListServ.js'

// 初始默认状态
const defultState = {
  xxx: '1',
  channelIndex: '1',
  // bannerList: [],
  bannerList: {
    1:[],
    2:[],
  },
  newBannerList: [{
    "url": "",
    "href": "",
    "type": 1,
    "ord": '',
    "published": '0',
    "channel": 101,
    "title": '轮播图'
  }],
  oVisible: false,
  otoTabCon: '',
  writeUrl: '',
  nowUrlChange: '',
  // pageTotal: '',
  pageTotal: {
  },
  pageInfo:{
    1:{
      pageNo: 1,
      pageSize: 10,
      total: 0,
    },
    2:{
      pageNo: 1,
      pageSize: 10,
      total: 0,
    },
  },

  newBanner: false,
  status: '',
  activeKey: '2',//路径面板tab索引
  mpUrlsObj:{
    mpUrls:[],
    expandedKeys:['0',],
    selectedKeys:[],
    onExpand:true,
    autoExpandParent:true,
    link:'',
    currentItem:'',
  }
}

// 声明module
const tmpModule = {
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if ('' + tmpModule.pathname === '' + pathname) {
          dispatch({ type: 'getBannerList', payload: {channelIndex:1} })
          dispatch({ type: 'getBannerList', payload: {channelIndex:2} })
        }
      })
    },
  },

  // 方法列表
  effects: {
    // 查询记录
    *queryList({ payload }, { put, call, select }) {
      try {
        console.log('this is list')
        return false
        let tmpState = yield select(state => state[tmpModule.namespace])
        const { data, resultCode, resultMsg } = yield call(getAdvertising, parse({ xxx: "1" }))
        yield put({
          type: 'updateStore',
          payload: {
            xxx: data
          }
        })
      } catch (e) {
        console.error(e)
      }
    },
    *getBannerList({ payload }, { put, call, select }) {
      let {channelIndex,bannerList,pageTotal,pageInfo} = yield select(state => state[tmpModule.namespace])
      channelIndex=payload.channelIndex||channelIndex
      let channel=channelIndex==1?'101':'201'
      payload.channel=channel
      let params={
        channel:channel,
        pageNo:payload.pageNo||pageInfo[channelIndex].pageNo||1,
        pageSize:payload.pageSize||pageInfo[channelIndex].pageSize||10,
      }
      try {
        console.warn(payload);
        // delete payload.channelIndex
        const { success, rows=[], total=0 ,pageNo,pageSize} = yield call(getBannerList, params)
        if (success) {
          bannerList[channelIndex]=rows
          pageInfo[channelIndex]={...pageInfo[channelIndex],pageNo,pageSize,total}
          pageTotal[channelIndex]=total
          yield put({ type: 'updateStore', payload: { bannerList, pageTotal,pageInfo } })
        }
      } catch (e) {
        console.log(e)
      }
    },
    *createBannerList({ payload }, { put, call, select }) {
      let {channelIndex} = yield select(state => state[tmpModule.namespace])
      try {
        console.warn(payload);
        let channel=channelIndex==1?'101':'201'
        payload.channel=channel
        let params={ pageNo: payload.pageNo, pageSize: payload.pageSize,channel }
        const { success } = yield call(createBannerList, parse(payload))
        if (success) {
          // const { success, rows, total } = yield call(getBannerList,{pageNo:1});
            yield put({ type: 'getBannerList', payload: {pageNo:1 } })
        }
      } catch (e) {
        console.log(e)
      }
    },

    *deleteBanner({ payload }, { put, call, select }) {
      let {channelIndex} = yield select(state => state[tmpModule.namespace])
      // channelIndex=payload.channelIndex||channelIndex
      let channel=channelIndex==1?'101':'201'
      payload.channel=channel
      try {
        console.warn(payload);
        const { success, rows } = yield call(deleteBanner, parse(payload))
        if (success) {
          message.success('删除成功！');
          yield put({ type: 'getBannerList', payload: {pageNo:1 } })
        }
      } catch (e) {
        message.error('删除失败，请稍后重试！');
        console.log(e)
      }
    },
    *changeBanner({ payload }, { put, call, select }) {
      let {channelIndex} = yield select(state => state[tmpModule.namespace])
      // channelIndex=payload.channelIndex||channelIndex
      let channel=channelIndex==1?'101':'201'
      payload.channel=channel
      try {
        console.warn(payload);
        const { success, rows } = yield call(changeBanner, parse(payload))
        if (success) {
          message.success('更改成功！');
          yield put({ type: 'getBannerList', payload: {pageNo:1 } })
        }
      } catch (e) {
        message.error('更改失败，请稍后重试！');
        console.log(e)
      }
    },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
